<?php

namespace backend\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Furniture;

/**
 * FurnitureSearch represents the model behind the search form of `backend\models\Furniture`.
 */
class FurnitureSearch extends Furniture
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'id_type', 'dimensions_height', 'dimensions_width', 'dimensions_depth', 'amount'], 'integer'],
            [['brand', 'scheme_img', 'color'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Furniture::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'id_type' => $this->id_type,
            'dimensions_height' => $this->dimensions_height,
            'dimensions_width' => $this->dimensions_width,
            'dimensions_depth' => $this->dimensions_depth,
            'amount' => $this->amount,
        ]);

        $query->andFilterWhere(['like', 'brand', $this->brand])
            ->andFilterWhere(['like', 'scheme_img', $this->scheme_img])
            ->andFilterWhere(['like', 'color', $this->color]);

        return $dataProvider;
    }
}
